package cn.crce.cloud9.comp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author OnCloud9
 * @description
 * @project NRCE-TEST
 * @date 2022年09月13日 下午 09:47
 */
public class Case41 extends JFrame implements ActionListener {
    private double x = 0;
    private double y = 0;
    JTextField xval = new JTextField(10);
    JButton calcBtn = new JButton("计算");
    JTextArea result = new JTextArea(10, 20);

    void initFrame() {
        Container content = getContentPane();
        content.setLayout(new BorderLayout());
        JPanel calcPanel = new JPanel();
        calcPanel.setLayout(new FlowLayout());
        calcPanel.add(new JLabel("角度"));
        calcPanel.add(xval);
        //***************************Found*********************
        calcPanel.add(calcBtn);
        content.add(calcPanel, "North");
        //***************************Found*********************
        calcBtn.addActionListener(this);
        content.add(result, "Center");
        //***************************Found*********************
        result.setEditable(false);
    }

    public Case41() {
        super("计算正弦函数");
        setSize(500, 200);
        initFrame();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        //***************************Found*********************
        if (e.getSource() == calcBtn) {
            x = Double.parseDouble(xval.getText()) / 180 * 3.14;
            y = Math.sin(x);
            //***************************Found*********************
            String str = "sin(" + xval.getText() + "*3.14/180)= " + y + '\n';
            //***************************Found*********************
            result.append(str);
        }
    }

    public static void main(String[] args) {
        new Case41();
    }
}
